package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima56;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;

public class SolrIndexCreateTest {

    @Test
    public void testCreateIndex() throws IOException, SolrServerException {
        //访问solr服务，直达核心
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建文档对象
        SolrInputDocument document = new SolrInputDocument();

        //给文档对象添加字段
        document.addField("id","10010");
        document.addField("title","使用solrJ给solr中加入索引数据");
        document.addField("price",123456);

        //把文档对象交给solr，保存
        solrServer.add(document);

        solrServer.commit();
    }


    @Test
    public void testCreateIndexWithBean() throws IOException, SolrServerException {
        //访问solr服务，直达核心
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        Heima56 heima56 = new Heima56();
        heima56.setId("16995");
        heima56.setTitle("黑马56 666");
        heima56.setPrice(12346456L);

        //把文档对象交给solr，保存
        solrServer.addBean(heima56);

        solrServer.commit();
    }


    @Test
    public void testUpdateIndex() throws IOException, SolrServerException {
        //访问solr服务，直达核心
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建文档对象
        SolrInputDocument document = new SolrInputDocument();

        //给文档对象添加字段
        document.addField("id","10010");
        document.addField("title","使用solrJ给solr中加入索引数据,测试修改");
        document.addField("price",123456);

        //把文档对象交给solr，保存
        solrServer.add(document);

        solrServer.commit();
    }
}
